package com.ruoyi.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.WxUser;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.core.domain.bo.UserBo;
import com.ruoyi.core.domain.vo.UserVo;
import com.ruoyi.core.domain.User;
import com.ruoyi.core.mapper.UserMapper;
import com.ruoyi.core.service.IUserService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户Service业务层处理
 *
 * @author xie
 * @date 2022-08-26
 */
@RequiredArgsConstructor
@Service
public class UserServiceImpl implements IUserService {

    private final UserMapper baseMapper;

    /**
     * 查询用户
     */
    @Override
    public UserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户列表
     */
    @Override
    public TableDataInfo<UserVo> queryPageList(UserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<User> lqw = buildQueryWrapper(bo);
        Page<UserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户列表
     */
    @Override
    public List<UserVo> queryList(UserBo bo) {
        LambdaQueryWrapper<User> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<User> buildQueryWrapper(UserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, User::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, User::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), User::getAppid, bo.getAppid());
        lqw.eq(bo.getPid() != null, User::getPid, bo.getPid());
        lqw.eq(StringUtils.isNotBlank(bo.getUnionid()), User::getUnionid, bo.getUnionid());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), User::getOpenid, bo.getOpenid());
        lqw.eq(StringUtils.isNotBlank(bo.getUtype()), User::getUtype, bo.getUtype());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), User::getTel, bo.getTel());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), User::getPassword, bo.getPassword());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), User::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), User::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), User::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), User::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), User::getAddress, bo.getAddress());
        lqw.eq(bo.getCredits() != null, User::getCredits, bo.getCredits());
        lqw.eq(bo.getTickets() != null, User::getTickets, bo.getTickets());
        lqw.eq(bo.getBalance() != null, User::getBalance, bo.getBalance());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentity()), User::getIdentity, bo.getIdentity());
        lqw.eq(bo.getLastLoginTime() != null, User::getLastLoginTime, bo.getLastLoginTime());
        String searchValue=bo.getSearchValue();
        if(StringUtils.isNotEmpty(searchValue)){
            lqw.and(w->w
                .or()
                .like(User::getDeptId,searchValue)
                .or()
                .like(User::getUserId,searchValue)
                .or()
                .like(User::getAppid,searchValue)
                .or()
                .like(User::getPid,searchValue)
                .or()
                .like(User::getUnionid,searchValue)
                .or()
                .like(User::getOpenid,searchValue)
                .or()
                .like(User::getUtype,searchValue)
                .or()
                .like(User::getTel,searchValue)
                .or()
                .like(User::getPassword,searchValue)
                .or()
                .like(User::getNickname,searchValue)
                .or()
                .like(User::getAvatar,searchValue)
                .or()
                .like(User::getSex,searchValue)
                .or()
                .like(User::getCity,searchValue)
                .or()
                .like(User::getAddress,searchValue)
                .or()
                .like(User::getCredits,searchValue)
                .or()
                .like(User::getTickets,searchValue)
                .or()
                .like(User::getBalance,searchValue)
                .or()
                .like(User::getIdentity,searchValue)
//                .or()
//                .like(User::getLastLoginTime,searchValue)
            );
        }
        return lqw;
    }


    /**
     * 新增用户
     */
    @Override
    public Boolean insertByBo(UserBo bo) {
        User add = BeanUtil.toBean(bo, User.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户
     */
    @Override
    public Boolean updateByBo(UserBo bo) {
        User update = BeanUtil.toBean(bo, User.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(User entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public UserVo getByOpenid(String openid) {
        LambdaQueryWrapper<User> query = Wrappers.lambdaQuery();
        query.eq(User::getOpenid,openid);
        return baseMapper.selectVoOne(query);
    }

    @Override
    public UserVo setWxUser(String unionid, String openid, String utype, String nickname, String avatar) {
        User user = new User();
        user.setUnionid(unionid);
        user.setOpenid(openid);
        user.setUtype(utype);
        user.setNickname(nickname);
        user.setAvatar(avatar);
        user.setIdentity("0");
        user.setLastLoginTime(new Date());
        baseMapper.insert(user);

        return BeanUtil.toBean(user,UserVo.class);
    }
}
